package cn.t.rpc.web;

import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.t.rpc.config.TRpcConfigProperties;
import cn.t.rpc.config.TRpcConfigProperties.ServiceType;
import cn.t.rpc.core.client.pool.TClientChannelPool;
import cn.t.rpc.core.server.holder.TLocalServiceHolderContext;
import cn.t.rpc.web.remote.ServerTimeService;

@RequestMapping("/rpc-monitor")
public class TRpcMonitorController {
	
	@Autowired
	private TRpcConfigProperties properties;
	
	@Autowired
	private ServerTimeService service;
	
	/**
	 * 框架信息
	 * @return 框架信息
	 */
	@RequestMapping("/monitor")
	@ResponseBody
	public Map<String, Object> monitor() {
		Map<String, Object> resultMap = new LinkedHashMap<>();
		resultMap.put("服务类型:", this.properties.getServiceType().getName());
		if (ServiceType.isServer(this.properties.getServiceType())) {
			
			Map<String, Object> serviceMap = new LinkedHashMap<>();
			serviceMap.put("提供服务的端口:", String.valueOf(this.properties.getPort()));
			serviceMap.put("提供的服务数量:", String.valueOf(TLocalServiceHolderContext.size()));
			serviceMap.put("提供的服务详情", TLocalServiceHolderContext.ServiceInfo());
			resultMap.put("服务", serviceMap);
			
			var pool = TClientChannelPool.instance();
			Map<String, Object> poolMap = new LinkedHashMap<>();
			poolMap.put("连接池详情", pool.getDescription());
			poolMap.put("可用连接数:", String.valueOf(pool.size()));
			resultMap.put("连接池", poolMap);
		}
		return resultMap;
	}
	
	/**
	 * 时间
	 */
	@RequestMapping("/time")
	@ResponseBody
	public String time() {
		return this.service.getServerTime("TMQ");
	}
}
